#!/usr/bin/perl

######################################################################
package Swignition::uF::figure;
######################################################################

use XML::LibXML qw(:all);
use strict;
use Swignition::MagicString;
use Swignition::uF;
use Swignition::GenericParser::Utils;

sub consume
{
	my $page = shift;
	
	return unless ($page->{Profiles}->{'http://purl.org/uF/figure/draft'}
		|| !$page->{options}->{uf_strict_profiles});
	
	$page->{uF}->{figure} = [ parse_all($page) ];
	foreach my $f (@{ $page->{uF}->{figure} })
		{ $page->mergeSubject( $f->rdf_subject ); }

	if (defined $page->{uF}->{figure}->[0])
		{ $page->mergeNS( $page->{uF}->{figure}->[0] ); }
}

sub parse_all
{
	my $page   = shift;
	my $within = shift || $page->{DOM};
	my @rv;
	
	my @nodes = searchClass('figure', $within);
	foreach my $figure (@nodes)
		{ push @rv, parse($page, $figure); }

	my @nodes = $within->getElementsByTagName('figure');
	foreach my $figure (@nodes)
		{ push @rv, parse($page, $figure); }
	
	return @rv;
} #/sub parse_all


sub uri
{
	my $this = shift;
	my $all  = shift;
	my @rv;

	if (length $this->{_id})
	{
		push @rv, $this->{_page}->uri.'#'.$this->{_id};
	}
	if (lc($this->{_dom}->tagName) eq 'body')
	{
		push @rv, $this->{_page}->uri;
	}
	push @rv, Swignition::GenericParser::Utils::BNodeURI($this->{_dom}, 'Figure');
	
	return $rv[0] unless (defined $all);
	while ($all) { shift @rv; $all--; }
	return @rv;
}


sub metadata_ns
{
	my $this = shift;
	return { 
		'foaf'     => { nsurl=>'http://xmlns.com/foaf/0.1/',title=>'FOAF' },
		'dcmitype' => { nsurl=>'http://purl.org/dc/dcmitype/',title=>'DCMI Types' },
		'dcterms'  => { nsurl=>'http://purl.org/dc/terms/',title=>'Dublin Core Terms' },
		'skos'     => { nsurl=>'http://www.w3.org/2004/02/skos/core#',title=>'Simple Knowledge Organization System' }
		};
} #/sub namespace


sub rdf_subject
{
	my $this    = shift;

	# Create RDF subject
	my $subject = Swignition::RDFModel::Subject->new($this->uri);
	$subject->addObject($this);
	$subject->setDom($this->{_dom});
	
	$subject->addVal('http://www.w3.org/1999/02/22-rdf-syntax-ns#type',
		Swignition::RDFModel::Value->new('http://purl.org/dc/dcmitype/Image', 1));
	$subject->addVal('http://www.w3.org/1999/02/22-rdf-syntax-ns#type',
		Swignition::RDFModel::Value->new('http://xmlns.com/foaf/0.1/Image', 1));

	foreach my $V (@{ $this->{legend} })
	{
		my $val = Swignition::RDFModel::Value->new($V);
		$subject->addVal('http://purl.org/dc/terms/description', $val);
	}
	
	if (length $this->{title})
	{
		my $val = Swignition::RDFModel::Value->new($this->{title});
		$subject->addVal('http://purl.org/dc/terms/title', $val);
	}
	
	if (length $this->{image})
	{
		my $val = Swignition::RDFModel::Value->new($this->{image}, 1);
		$subject->addVal('http://purl.org/dc/terms/source', $val);
	}
	
	if (length $this->{alt})
	{
		my $val = Swignition::RDFModel::Value->new($this->{alt});
		$subject->addVal('http://www.w3.org/2004/02/skos/core#altLabel', $val);
	}

	foreach my $V (@{ $this->{credit} })
	{
		my $val;
		if (ref $V && UNIVERSAL::can($V, 'uri')) 
			{ $val = Swignition::RDFModel::Value->new($V->uri, 1); }
		else
			{ $val = Swignition::RDFModel::Value->new($V); }
		
		$subject->addVal('http://purl.org/dc/terms/contributor', $val);
	}
	
	foreach my $V (@{ $this->{subject} })
	{
		my $val;
		if (ref $V && UNIVERSAL::can($V, 'uri')) 
			{ $val = Swignition::RDFModel::Value->new($V->uri, 1); }
		else
			{ $val = Swignition::RDFModel::Value->new($V); }
		
		$subject->addVal('http://xmlns.com/foaf/0.1/depicts', $val);
	}

	foreach my $V (@{ $this->{license} })
	{
		my $val;
		if (UNIVERSAL::can($V, 'uri'))
			{ $val = Swignition::RDFModel::Value->new($V->uri, 1); }
		else
			{ $val = Swignition::RDFModel::Value->new($V); }
		$subject->addVal('http://creativecommons.org/ns#license', $val);
		$subject->addVal('http://purl.org/dc/terms/license', $val);
	}

	foreach my $tag (@{$this->{category}})
	{
		$subject->addVal('http://www.holygoat.co.uk/owl/redwood/0.1/tags/taggedWithTag',
			Swignition::RDFModel::Value->new($tag->uri, 1));
		$subject->addVal('http://www.holygoat.co.uk/owl/redwood/0.1/tags/tag',
			Swignition::RDFModel::Value->new($tag->uri_tagging, 1));
	}

	return $subject;
	
} #/sub rdf_subject


sub parse
{
	my $page = shift;
	my $rv = { '_dom'=>shift };
	my $pkg = __PACKAGE__;
	if (defined $page->{uF}->{_Shortcuts}->{$pkg}->{ $rv->{_dom}->getAttribute('_xpath') })
		{ return $page->{uF}->{_Shortcuts}->{$pkg}->{ $rv->{_dom}->getAttribute('_xpath') }; }
	else
		{ $page->{uF}->{_Shortcuts}->{$pkg}->{ $rv->{_dom}->getAttribute('_xpath') } = $rv; }
		
	my $figure_node = $rv->{'_dom'}->cloneNode(1);

	my $id = $figure_node->getAttribute('id');
	$rv->{_id} = $id if (length $id);
	$rv->{_page} = $page;
	
	data_patterns($page, $figure_node);

	# Funny little @longdesc include thingy.
	my $img;
	if ($figure_node->localname eq 'img')
		{ $img = $figure_node; }
	else
	{
		my @images = searchClass('image', $figure_node);
		@images = $figure_node->getElementsByTagName('img')
			unless (@images);
		$img = $images[0];
	}
	if ($img)
	{
		my $longdesc = $img->getAttribute('longdesc');
		if ($longdesc =~ /^\#(.*)$/)
		{
			my $dom = searchID($1, $page->{DOM}->documentElement);
			my $pth = $dom->getAttribute('_xpath');
			
			if ($dom && ($pth ne substr($figure_node->getAttribute('_xpath'),0,length($pth))))
			{
				my $clone = $dom->cloneNode(1);
				$figure_node->addChild($clone);
			}
		}
	}
	
	simple_parse(
		$rv,
		$figure_node,
		[
			['credit',    'M*',  {embedded=>'hCard'}],
			['subject',   'M*',  {embedded=>'hCard'}]
		],
		{
			'rel-tag'     => 'category'
		}
	);

	# The 'image' property's rules are too complex for simple_parse().
	$img = undef;
	if ($figure_node->localname eq 'img')
		{ $img = $figure_node; }
	else
	{
		my @images = searchClass('image', $figure_node);
		@images = $figure_node->getElementsByTagName('img')
			unless (@images);
		$img = $images[0];
	}
	if ($img)
	{
		$rv->{image} =   ($page->uri($img->getAttribute('src')));
		$rv->{title} = ms($img->getAttribute('title'), $img);
		$rv->{imgid} =   ($img->getAttribute('id'));
		$rv->{alt}   = ms($img->getAttribute('alt'), $img);
	}
	
	# The 'legend' property's rules are too complex for simple_parse().
	my @legends;
	if ($figure_node->getAttribute('class') =~ /\b(legend)\b/)
	{
		push @legends, $figure_node;
	}
	else
	{
		@legends = searchClass('legend', $figure_node);
		my @elems = $figure_node->getElementsByTagName('legend');
		foreach my $e (@elems)
		{
			push @legends, $e
				unless ($e->getAttribute('class') =~ /\b(legend)\b/);
		}
	}
	foreach my $legend (@legends)
	{
		my $value = STRINGIFY($legend, 'value');
		$value = ms($img->getAttribute('title'), $img)
			if ($legend==$img);
		push @{$rv->{'legend'}}, $value;
	}
		
	bless $rv;	
	return $rv;
	
} #/sub parse


1; #/package Swignition::uF::figure




